import request from '@/utils/request'
import { parseTime } from '@/utils/index'

/*
获取雪球cookie
*/
export function getXueqiuCookie() {
	return request({
		url: '/ajax/get-cookie',
		method: 'post',
        data: {
            url: 'https://www.xueqiu.com',
            header: [
                {key: 'User-Agent', val: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55'}
            ]
        }
	})
}

export function getXueqiuCookieByCache() {
    let _cacheKey = 'www-xueqiu-cookies'
    let _cache = sessionStorage.getItem(_cacheKey)
    if (_cache) {
        console.log('getXueqiuCookie...cache', _cache)
        return _cache
    }

    _cache = ''
    getXueqiuCookie().then((res) => {
        console.log('getXueqiuCookie...start')
        let _xueqiuCookies = res.data || []
        if (_xueqiuCookies.length > 0) {
            _xueqiuCookies.forEach(row => {
                if (row.Name && row.Value) {
                    if (_cache) {
                        _cache += '; '
                    }
                    _cache += row.Name + '=' + row.Value
                }
            })
        }

        sessionStorage.setItem(_cacheKey, _cache)
        console.log('getXueqiuCookie...finish', _cache)
        return getXueqiuCookieByCache()
    })
}

/*
国债:sh019547
https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=SH019547&begin=1642237251899&period=day&type=before&count=-284&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance
*/
export function getXueqiuSh019547() {
	return request({
		url: '/ajax/do-get',
		method: 'post',
        data: {
            url: 'https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=SH019547&begin='+(new Date() * 1 + 60 * 60 * 24 * 1000)+'&period=day&type=before&count=-1250&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance',
            header: [
                {key: 'Content-Type', val: 'application/json'},
                {key: 'Referer', val: 'https://xueqiu.com'},
                {key: 'User-Agent', val: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55'},
                // {key: 'cookie', val: 'device_id=165abdd1fdbc0e9878f4ba5503f723ea; s=c3114ambir; xq_is_login=1; u=5517310191; bid=98e59204c568ca9385a61a7c4c70a5e4_kwyu0bmi; xq_a_token=03f926316a67e02fac31867143d2ea7942a23c41; xqat=03f926316a67e02fac31867143d2ea7942a23c41; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjU1MTczMTAxOTEsImlzcyI6InVjIiwiZXhwIjoxNjQ0NzQyODA1LCJjdG0iOjE2NDIxNTA4MDU1MDAsImNpZCI6ImQ5ZDBuNEFadXAifQ.LE6CbPPCWI0QS8AB0HeDOv6MqgnW0BaeCpQ_DAd_nWcIhHapAraHiQa6xUss52EZ__TEl3NypJd6sRzUIxEExBcfqKZQMGvG24SvwQGX06imnzF2aiVh1Hk16lUn9PY7bWiSwov7bp-KBiaeu9hnrZKUWDrzub9MnNYueY0XExLhejSauTkICEZBue_JJXoRn23bWVyz_91YzLaPpnokDUTk-qNH-sTRB38mgNulqnS466AWyLRjlrZsxdUiqmKIdapjZvqiU4Bl-5lpSvWUxqhpetugArkabmFu_02gh3YALTWuXp65WKy74LBt6OWiuvFdOixw1A68x3ivt7Vhxg; xq_r_token=ca342829a91030cdd6b50041735a49b0f92e5d2f; Hm_lvt_1db88642e346389874251b5a1eded6e3=1640848779,1642150807,1642150821; is_overseas=0; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1642385865'}
                {key: 'cookie', val: getXueqiuCookieByCache()}
            ]
        }
	})
}

// 16国债19、2046-8-22到期 ytm到期收益率=[年利息+(面值-买入价格) / 剩余年限] / 买入价格 * 100%
export function getXueqiuSh019547Ytm(timestamp, price) {
    let _interest = 0; //应计利息
    let _year = parseTime(timestamp, '{y}')
    if (timestamp > new Date(_year + "/8/23").getTime()) {
        let _lastDt = new Date(_year + "/8/23").getTime()
        _interest = (timestamp - _lastDt)/(1000*60*60*24)*3.27/365
    } else if (timestamp > new Date(_year + "/2/23").getTime()) {
        let _lastDt = new Date(_year + "/2/23").getTime()
        _interest = (timestamp - _lastDt)/(1000*60*60*24)*3.27/365
    } else {
        let _lastDt = new Date(_year - 1 + "/8/23").getTime()
        _interest = (timestamp - _lastDt)/(1000*60*60*24)*3.27/365
    }
    let _fullPrice = price
    if (_interest > 0) {
        _fullPrice += _interest
    }
    //到期收益率=[年利息+(面值-买入价格) / 剩余年限] / 买入价格 * 100%
    let _lessYear = (2418480000000-timestamp)/(1000*60*60*24*365) //剩余年限
    let _ytm = (3.27 + (100 - _fullPrice) / _lessYear) / _fullPrice * 100

    return _ytm
}

/*
获取指定的多个代码的行情数据
https://xueqiu.com/service/v5/stock/batch/quote?symbol=SH000001,SH000016,SH000300,SH000905&_=1642736121381
*/
export function getXueqiuBatchQuote(symbol) {
    if (!symbol) {
        symbol = 'SH000001'
    }
	return request({
		url: '/ajax/do-get',
		method: 'post',
        data: {
            url: 'https://xueqiu.com/service/v5/stock/batch/quote?symbol='+symbol+'&_=1642736121381'+(new Date() * 1),
            header: [
                {key: 'Content-Type', val: 'application/json'},
                {key: 'Referer', val: 'https://xueqiu.com'},
                {key: 'User-Agent', val: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55'},
                {key: 'cookie', val: getXueqiuCookieByCache()}
            ]
        }
	})
}
